<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1UserWithdrawalAccount;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 提现账号
 */
class V1UserWithdrawalAccountController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = ['select2'];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1UserWithdrawalAccount
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1UserWithdrawalAccount;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        $admin = admin();
        return view('v1-user-withdrawal-account/index', compact('admin'));
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select2(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput2($request);
        $admin = admin();
        if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
            $where['user_id'] = $admin['id'];
        }
        $query = $this->doSelect($where, $field, $order);
        $query->select(['id','account as name']);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $data = $this->insertInput($request);

            $admin = admin();
            if ($admin['user_status'] == CommonConstant::DEFAULT_1) {
                $user_id = $request->post('user_id');
                if (!$user_id) {
                    return $this->json(1, '会员ID参数错误');
                }
                $userInfo = (array)Util::db()
                    ->table('wa_admins')
                    ->where('id', $user_id)
                    ->select(['id', 'truename'])
                    ->first();
            }
            if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
                $userInfo = $admin;
            }
            if (!$userInfo) {
                return $this->json(1, '会员不存在或已删除！');
            }
            if ($data['name'] != $userInfo['truename']) {
                return $this->json(1, '提现姓名必须与实名认证姓名相同！');
            }
            $data['user_id'] = $userInfo['id'];
            $data['user_name'] = $userInfo['username'];
            $id = $this->doInsert($data);
            return $this->json(0, 'ok', ['id' => $id]);
        }
        $admin = admin();
        return view('v1-user-withdrawal-account/insert', compact('admin'));
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $data = $this->inputFilter($request->post());

            $admin = admin();
            if ($admin['user_status'] == CommonConstant::DEFAULT_1) {
                $user_id = $request->post('user_id');
                if (!$user_id) {
                    return $this->json(1, '会员ID参数错误');
                }
                $userInfo = (array)Util::db()
                    ->table('wa_admins')
                    ->where('id', $user_id)
                    ->select(['id', 'truename'])
                    ->first();
            }
            if ($admin['user_status'] == CommonConstant::DEFAULT_2) {
                $userInfo = $admin;
            }
            if (!$userInfo) {
                return $this->json(1, '会员不存在或已删除！');
            }
            if ($data['name'] != $userInfo['truename']) {
                return $this->json(1, '提现姓名必须与实名认证姓名相同！');
            }
            return parent::update($request);
        }
        $admin = admin();
        return view('v1-user-withdrawal-account/update', compact('admin'));
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDelete($ids);
        return $this->json(0);
    }

}
